﻿using Api.WDS.Methods;
using Method.DAL;
using Models.operResult;
using Models.VModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Operate
{
    public class SearchOp
    {
        /// <summary>
        /// 实时模糊查询
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public OperResult<List<string>> RealLikeText(string text)
        {
            if (string.IsNullOrEmpty(text))
                return OperResult<List<string>>.Succeed(new List<string>(0));

            return OperResult<List<string>>.Succeed(new SearchMet().RealLikeText(text));
        }

        /// <summary>
        /// 添加关键词搜索次数
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public OperResult InsertHeat(string text)
        {
            if (string.IsNullOrEmpty(text))
                return OperResult.Failed();
            if (new SearchMet().InsertHeat(text))
                return OperResult.Succeed();
            return OperResult.Failed();
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        public OperResult<List<V_Search>> FindResult(string token, string userC, string artC, string text)
        {
            if (string.IsNullOrEmpty(text))
                return OperResult<List<V_Search>>.Failed("文本禁止为空");
            var uid = new GetUidByToken().GetUid(token);
            var ucount = 0;
            if (int.TryParse(userC, out int i))
                ucount = Convert.ToInt32(userC);
            var acount = 0;
            if (int.TryParse(artC, out int l))
                acount = Convert.ToInt32(artC);

            var list = new SearchMet().FindResult(uid, ucount, acount, text.ToLower());
            if (list.Count == 0)
                return OperResult<List<V_Search>>.Failed("没有查询到");
            return OperResult<List<V_Search>>.Succeed(list);
        }
    }
}
